//
//  AddCourseCell.m
//  优学荟管理系统
//
//  Created by 星道 on 17/8/24.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "AddCourseCell.h"

@implementation AddCourseCell
{
    IBOutletCollection(UITextField) NSArray *_textArr;
    IBOutlet NSLayoutConstraint *_CourseTitleTop;
    IBOutlet UIView *_smallCategoryView;
}

- (void)awakeFromNib {

    [super awakeFromNib];

    
    self.courseBigCategory_Text.delegate    = self;
    self.courseSmallCategory_Text.delegate  = self;
    self.courseTwoSmallCategory_Text.delegate = self;
    self.province.delegate                  = self;
    self.city.delegate                      = self;
    self.area.delegate                      = self;
    self.evaMould_Text.delegate             = self;
    self.Des_Text.delegate                  = self;
    self.courseBeginTime_Text.delegate = self;
    self.courseEndTime_Text.delegate = self;
    self.courseTime_Text.delegate = self;
    self.price_Text.delegate = self;
    self.coins_Text.delegate = self;
    self.ShowPrice.delegate = self;
    self.deleteCourse.hidden = YES;
    
    self.coursePeople_Text.placeholder = @"请输入报名人数上限（可不填）";
    
    
    
    
    
    
}


/** 单击输入框触发事件,tag值详见.h文件注释 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
 if (textField == self.price_Text) {
     
     //self.deductionCoins_Text.text = textField.text;

     
     return YES;
 }else if (textField == self.coins_Text){
     return YES;
 }else if (textField == self.ShowPrice) {
     return YES;
 }
    else
    {
    [self.Delegate cell:self ClickWithTextField:textField tag:textField.tag];
    return NO;
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    NSInteger money = [textField.text  integerValue];

  
    if (textField == self.price_Text) {

        if (money *0.05  >= 200) {//抵扣金额超过200的情况
            money = 200;//最大抵扣星币
            self.deductionCoins_Text.text = [NSString stringWithFormat:@"%ld",(long)money];
       
        }else{//抵扣金额小于课程
            
            NSInteger moneyTow  =  money *0.05;
    
           self.deductionCoins_Text.text = [NSString stringWithFormat:@"%ld",(long)moneyTow];
        }

        
    }else if (textField == self.coins_Text){
        
        if ([textField.text integerValue] > [self.deductionCoins_Text.text integerValue]) {//星币抵扣数大于最大可抵扣的星币数时，给个提示
            //给个提示
            [MBProgressHUD  showError:@"星币抵扣不能大于最大抵扣星币"];
            textField.text = nil;
            
        }else{
           
            
        }
        
    }
   
    
    
    
}


- (IBAction)sure:(UIButton *)sender {

    __block BOOL isRight = NO;
    [_textArr enumerateObjectsUsingBlock:^(UITextField * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if (obj.text.length <= 0) {

            isRight = YES;
            obj.layer.borderColor = [UIColor redColor].CGColor;
            obj.layer.borderWidth = 1.f;
        } else {

            obj.layer.borderColor = [UIColor clearColor].CGColor;
            obj.layer.borderWidth = 0;
        }
    }];

    if (isRight) {

        [HUD showError:@"请完善红框内容"];
    } else  {

        [self.Delegate cell:self ClickSureWithButton:sender];
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    self._textViewAlpaceholder.hidden = textView.text.length;
}


/** 添加小分类 */
- (void)AddNewSmallCategory:(UIButton *)sender {
    // 可选小分类的出现条件必须是小分类先选择
    if (self.courseSmallCategory_Text.text.length <= 0) {
        [HUD showError:@"请选择小分类"];
        return;
    }
    // 动画
    sender.selected = !sender.selected;
    sender.selected ? [UIView animateWithDuration:0.3 animations:^{
        _CourseTitleTop.constant = 38;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            _smallCategoryView.alpha = 1;
        }];
    }] : [UIView animateWithDuration:0.3 animations:^{
        _smallCategoryView.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            _CourseTitleTop.constant = 8;
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            self.courseTwoSmallCategory_Text.text = @"";
            self.smallTwoID = @"";
        }];
    }];
}

/** 删除课程 */
- (IBAction)DeleteCourse:(UIButton *)sender {

    self.ClickDeleteCourse ? self.ClickDeleteCourse () : nil;
}
@end
